package FRM;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

import Corba.EnchereServeurSingleton;
import DAO.UtilisateurDAO;
import Encheres.Model.User;
import GestionParametres.GST_Utilisateur;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FRM_Connexion extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField TXT_Pseudo;
	private JPasswordField PWD_MotDePasse;
	private JButton BTN_Connexion;
	private JButton BTN_Quitter;

	/**
	 * This method initializes
	 * 
	 */
	public FRM_Connexion() {
		super();
		setResizable(false);
		initialize();
		setTitle("Connexion");
		setSize(250, 300);
		setLocationRelativeTo(null);

		getContentPane().setLayout(new BorderLayout());

		JPanel PNL_Formulaire = new JPanel(
				new FormLayout(new ColumnSpec[] {
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,}, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		getContentPane().add(PNL_Formulaire);
						JLabel LBL_Pseudo = new JLabel("Pseudo : ");
						PNL_Formulaire.add(LBL_Pseudo, "4, 3, right, default");
				
						TXT_Pseudo = new JTextField();
						PNL_Formulaire.add(TXT_Pseudo, "6, 3, left, default");
						TXT_Pseudo.setColumns(15);
						
								JLabel LBL_MotDePasse = new JLabel("Mot de passe : ");
								PNL_Formulaire.add(LBL_MotDePasse, "4, 5, right, default");
						
								PWD_MotDePasse = new JPasswordField();
								PWD_MotDePasse.setColumns(15);
								PNL_Formulaire.add(PWD_MotDePasse, "6, 5, left, default");
						
						JLabel lblPasEncoreInscrit = new JLabel("Pas encore inscrit ?");
						lblPasEncoreInscrit.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent arg0) {
								FRM_Connexion.this.inscription();
							}
						});
						lblPasEncoreInscrit.setForeground(Color.BLUE);
						lblPasEncoreInscrit.setHorizontalAlignment(SwingConstants.RIGHT);
						PNL_Formulaire.add(lblPasEncoreInscrit, "6, 7");

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel, BorderLayout.SOUTH);

		BTN_Connexion = new JButton("Connexion");
		BTN_Connexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FRM_Connexion.this.Connexion();
			}
		});
		panel.add(BTN_Connexion);

		BTN_Quitter = new JButton("Quitter");
		BTN_Quitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FRM_Connexion.this.Quitter();
			}
		});
		panel.add(BTN_Quitter);
		
		JPanel PNL_Image = new JPanel();
		getContentPane().add(PNL_Image, BorderLayout.NORTH);
		PNL_Image.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setIcon(new ImageIcon(FRM_Connexion.class.getResource("/Images/Cart_128.png")));
		PNL_Image.add(lblNewLabel, BorderLayout.CENTER);
		
		JLabel lblNewLabel_1 = new JLabel("Encheres et en os");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_1.setVerticalAlignment(SwingConstants.BOTTOM);
		PNL_Image.add(lblNewLabel_1, BorderLayout.SOUTH);
	}

	protected void inscription() {
		FRM_Inscription inscr = new FRM_Inscription();
		inscr.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setTitle("Connexion");
		this.setSize(new Dimension(400, 240));
		this.setVisible(false);

	}

	protected void Quitter() {
		EnchereServeurSingleton.getInstance().deconnectService();
		this.dispose();
	}

	protected void Connexion() {
		if (GST_Utilisateur.connexionUtilisateurValid(TXT_Pseudo.getText(),
				String.valueOf(PWD_MotDePasse.getPassword()))) {
			FRM_Principale frmPrinc = new FRM_Principale();
			frmPrinc.setVisible(true);
			this.dispose();
		} else {
			javax.swing.JOptionPane.showMessageDialog(null,
					"Identifiant ou mot de passe incorrect");
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"
